import logging

from django.conf import settings
import datetime

from django.core.mail import EmailMultiAlternatives

### 0、用于生 html 表格
def generate_html_table(data):
    html = '<table border="1" style="border-collapse: collapse;">'
    html += '<tr><th>日期</th><th>保单类型</th><th>保单本数</th><th>异常笔数</th><th>成功率</th></tr>'

    for item in data:
        if len(item) == 5:
            html += f'<tr>'
            html += f'<td>{item["date"]}</td>'
            html += f'<td>{item["policy_type"]}</td>'
            html += f'<td>{item["policy_count"]}</td>'
            html += f'<td>{item["abnormal_count"]}</td>'
            html += f'<td>{item["success_rate"]}</td>'
            html += '</tr>'
        else:
            html += f'<tr>'
            html += f'<td>{item["date"]}</td>'
            html += f'<td>{item["policy_type"]}</td>'
            html += f'<td>{item["policy_count"]}</td>'
            html += f'<td>{item["abnormal_count"]}</td>'
            html += f'<td>{item["success_rate"]}</td>'
            html += f'<td>{item["shida"]}</td>'
            html += f'<td>{item["lirenhang"]}</td>'
            html += f'<td>{item["donggang"]}</td>'
            html += '</tr>'

    html += '</table>'
    return html

###   1、日常数据统计
def generate_table_daily_summary1():
    ## 从数据库中读取出来的数据
    data1=[
        {'date':'2025/09/01','policy_type':'个险电子','policy_cout':6464,'abnormal_count':0,'success_rate':'100%'},
        {'date':'2025/09/01','policy_type':'个险纸质','policy_cout':3076,'abnormal_count':0,'success_rate':'100%','shida':2047,'lirenhang':929,'donggang':100},
        {'date':'2025/09/01','policy_type':'新核心单证','policy_cout':33234,'abnormal_count':0,'success_rate':'100%'},
        {'date':'2025/09/01','policy_type':'团险电子','policy_cout':7449,'abnormal_count':0,'success_rate':'100%'},
        {'date':'2025/09/01','policy_type':'团险纸质','policy_cout':1066,'abnormal_count':0,'success_rate':'100%','shida':808,'lirenhang':205,'donggang':8},
    ]
    ## 生成 HTML 表格
    html1_table1 = generate_html_table(data1)

    return html1_table1

###  2、硬盘空间监控
def generate_table_daily_summary2():
    ## 从数据库中读取出来的数据
    data2 = [
        {'iam': '10.1.188.4', 'free_size':'722.0G' , 'disk_total': '999.9G', 'free_rate': '72%'},
        {'iam': '10.1.188.5', 'free_size':'896.0G' , 'disk_total': '999.9G', 'free_rate': '90%'},
        {'iam': '10.1.188.7', 'free_size':'406.0G' , 'disk_total': '460.0G', 'free_rate': '88%'},
        {'iam': '10.1.188.8', 'free_size':'382.0G' , 'disk_total': '460.0G', 'free_rate': '83%'}
        ]

    ## 生成 HTML 表格
    html = '<table border="1" style="border-collapse: collapse;">'
    html += '<tr><th>服务器</th><th>D盘剩余空间</th><th>D盘总容量</th><th>D盘空余比列</th></tr>'

    for item in data2:
        html += f'<tr>'
        html += f'<td>{item["iam"]}</td>'
        html += f'<td>{item["free_size"]}</td>'
        html += f'<td>{item["disk_total"]}</td>'
        html += f'<td>{item["free_rate"]}</td>'
        html += '</tr>'

    html += '</table>'
    return html


###  3、数据库空间监控
def generate_table_daily_summary3():
    ## 从数据库中读取出来的数据
    dict_db={'tablename':'SIP_PRS', 'tabel_size':'324268982272', 'table_free':'234190012416','table_use':'90078969856',
             'tabel_size_g':'3.24268982272G', 'table_free_g':'218.11G',  'table_use_g':'83.89G',
             'free_rate':'27.78%'}



    ## 生成 HTML 表格
    html = '<table border="1" style="border-collapse: collapse;">'
    html += '<tr><th>表名空间名</th><th>表空间大小</th><th>表空间剩余大小</th><th>表空间使用大小</th><th>表空间大小(G)</th><th>表空间剩余大小(G)</th><th>表空间使用大小(G)</th></tr>'
    html += f'<tr>'
    html += f'<td>{dict_db["tablename"]}</td>' + f'<td>{dict_db["tabel_size"]}</td>' + f'<td>{dict_db["table_free"]}</td>' + f'<td>{dict_db["table_use"]}</td>'
    html += f'<td>{dict_db["tabel_size_g"]}</td>' + f'<td>{dict_db["table_free_g"]}</td>' + f'<td>{dict_db["table_use_g"]}</td>' + f'<td>{dict_db["free_rate"]}</td>'
    html += '</tr>'
    html += '</table>'
    return html


def send_email(task,tasklog):
    logging.info('run send_email start!')
    print('======================================run send_email start!')
    try:
        from_email = settings.DEFAULT_FROM_EMAIL
        resultid = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
        # 获取收件人
        if task.toemail:
            to_email = task.toemail
            to_emaillist = to_email.split(';')
            print(to_emaillist)
        else:
            to_emaillist=[]
        # 获取抄送人
        if task.ccemail:
            cc_email = task.ccemail
            cc_emaillist = cc_email.split(';')
            print(cc_emaillist)
        else:
            cc_emaillist=[]
        ### 固定部分
        subject = tasklog.taskplanname + ':'
        text_content = ""

        ###   1、日常数据统计
        msg = EmailMultiAlternatives(subject, text_content, from_email, to_emaillist, None, None, None, None, None,
                                     cc_emaillist)

        ###  2、硬盘空间监控


        ###  3、数据库空间监控

        ## 不是 xls的时候走这里 即(CSV格式)
    except Exception as e:
        logging.error('e')

    return msg.send()
